#include <QtGui>
#include <QPalette>
#include <QMessageBox>
#include "countdown.h"

CountdownClock::CountdownClock(QWidget *parent, int number): QLCDNumber(parent)
{
    setSegmentStyle(Filled);
    setDigitCount(number);
    this->number=number;

    QTimer *timer = new QTimer(this);

    start_time = QTime::currentTime();
    target_time = start_time.addSecs(60);

    connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));

    this->setObjectName(tr("COUNT DOWN CLOCK"));

    timer->start(1000);
    showTime();

    QPalette *pp = new QPalette();
    pp->setColor(QPalette::WindowText, Qt::red);
    this->setPalette(*pp);

    this->setFrameStyle(QFrame::NoFrame);

    //    resize(parent->width(),parent->height());
    resize(200,50);
}

void CountdownClock::showTime()
{
    QString text = "";

    int msecs = ceil((QTime::currentTime().msecsTo(target_time)/1000.0)) * 1000;

    QString formattedTime;

    int hours = msecs/(1000*60*60);
    int minutes = (msecs-(hours*1000*60*60))/(1000*60);
    int seconds = (msecs-(minutes*1000*60)-(hours*1000*60*60))/1000;
    //       int milliseconds = msecs-(seconds*1000)-(minutes*1000*60)-(hours*1000*60*60);

    formattedTime.append(QString("%1").arg(hours, 2, 10, QLatin1Char('0')) + ":" +
                         QString( "%1" ).arg(minutes, 2, 10, QLatin1Char('0')) + ":" +
                         QString( "%1" ).arg(seconds, 2, 10, QLatin1Char('0')) );

    text = formattedTime;

    display(text);
}

